package com.harmontronics.hanma.paas.paas.domain.tenant.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.consts.DbConstants;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.valueobject.Fields;
import com.harmontronics.hanma.paas.paas.domain.tenant.repository.persistence.HmOperationLogRepository;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.HmOperationLog;
import com.harmontronics.hanma.paas.paas.utils.EscapeUtil;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.tenant.repository.mapper.HmOperationLogMapper;
import com.harmontronics.hanma.paas.paas.domain.tenant.repository.po.HmOperationLogPO;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("hmOperationLogService")
public class HmOperationLogService {

    @Autowired
    private HmOperationLogRepository hmOperationLogRepository;


    public HmOperationLog findById(Long id) {
        HmOperationLogPO po = hmOperationLogRepository.getById(id);
        return BeanUtil.toBean(po, HmOperationLog.class);
    }

    public void save(HmOperationLog hmOperationLog) {
        HmOperationLogPO po = BeanUtil.toBean(hmOperationLog, HmOperationLogPO.class);
        hmOperationLogRepository.save(po);
    }

    public void update(HmOperationLog hmOperationLog) {
        HmOperationLogPO po = BeanUtil.toBean(hmOperationLog, HmOperationLogPO.class);
        hmOperationLogRepository.updateById(po);
    }

    public void remove(List<Long> ids) {
        hmOperationLogRepository.removeByIds(ids);
    }

    public List<HmOperationLog> getList() {
        List<HmOperationLogPO> pos = hmOperationLogRepository.list();
        return Convert.toList(HmOperationLog.class, pos);
    }

    public PageBase<HmOperationLog> getList(String submitter, LocalDateTime startTime, LocalDateTime endTime, int pageNo, int pageSize) {
        Page<HmOperationLogPO> page = new Page<>(pageNo, pageSize);
        QueryWrapper<HmOperationLogPO> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(submitter)) {
            queryWrapper.like(Fields.TENANT_USER_NAME, EscapeUtil.escapeChar(submitter));
        }
        if (startTime != null) {
            queryWrapper.gt(DbConstants.CREATE_DATE, startTime);
        }
        if (endTime != null) {
            queryWrapper.lt(DbConstants.CREATE_DATE, endTime);
        }
        String appUnique = RequestUtils.getAppUnique();
        if (!StringUtils.isEmpty(appUnique) && !DbConstants.DS_MASTER_NAME.equals(appUnique)) {
            queryWrapper.eq(Fields.APP_UNIQUE, appUnique.split("_")[1]);
        }
        queryWrapper.orderByDesc(DbConstants.CREATE_DATE);
        IPage<HmOperationLogPO> resultPage =
                hmOperationLogRepository.selectPage(queryWrapper, page);
        return PageBase.from(resultPage, HmOperationLog.class);
    }

}